package test.yzx.pproject.act3;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import test.yzx.pproject.R;


public class FragmentTest1Activity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewGroup layout = new LinearLayout(this);
        layout.setId(R.id.layout_id_1);
        layout.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
        setContentView(layout);

        Fragment1 f = new Fragment1();
        f.setTextColor(Color.WHITE);
        FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        t.add(R.id.layout_id_1, f,"xxx");
        t.commitNow();
        Log.e("---------------->>", "**********************over**********************");

    }




    public static class Fragment1 extends Fragment{
        private int mTextColor = Color.WHITE;
        public void setTextColor(int mTextColor) {
            this.mTextColor = mTextColor;
        }
        @Override
        public void onSaveInstanceState(Bundle outState) {
            outState.putInt("textColor", mTextColor);
            super.onSaveInstanceState(outState);
        }


        private void l(String s){
            Log.e("--------->>", s);
        }
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            l("on attach");
        }
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            l("on create");
            if(savedInstanceState != null){
                int textColor = savedInstanceState.getInt("textColor");
                if(textColor > 0)
                    mTextColor = textColor;
            }
        }
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            l("on create view");
            final TextView tv = new TextView(getContext());
            tv.setTextSize(50);
            tv.setTextColor(mTextColor);
            tv.setText(mTextColor+"");
            tv.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    FragmentManager fm = getActivity().getSupportFragmentManager();
                    FragmentTransaction t = fm.beginTransaction();
                    ViewCompat.setTransitionName(tv, "scaleX");
                    t.addSharedElement(tv, "scaleX");
                    Fragment1 f2 = new Fragment1();
                    f2.setTextColor(Color.YELLOW);
                    t.add(R.id.layout_id_1, f2,"f2").hide(Fragment1.this).commitNow();
                }
            });
            return tv;
        }
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            l("on activity created");
            super.onActivityCreated(savedInstanceState);
        }
        @Override
        public void onStart() {
            super.onStart();
            l("on start");
        }
        @Override
        public void onStop() {
            l("on stop");
            super.onStop();
        }
        @Override
        public void onDestroyView() {
            l("on destroy view");
            super.onDestroyView();
        }
        @Override
        public void onDestroy() {
            l("on destroy");
            super.onDestroy();
        }
        @Override
        public void onDetach() {
            l("on detach");
            super.onDetach();
        }
    }

}
